package com.xiuxia.bopicturebackend.controller;


import com.xiuxia.bopicturebackend.common.BaseResponse;
import com.xiuxia.bopicturebackend.common.MinIOUtil;
import com.xiuxia.bopicturebackend.common.ResultUtils;
import com.xiuxia.bopicturebackend.config.MinioConfig;
import com.xiuxia.bopicturebackend.exception.ErrorCode;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/minio")
@AllArgsConstructor
public class FileUploadController {
    private MinioConfig minioConfig;
    private MinIOUtil minIOUtil;

    @GetMapping("/bucketExists")
    @ApiOperation("判断存储桶是否存在")
    public BaseResponse bucketExists(@RequestParam("bucketName") String bucketName) {
        Map<String, Object> map = new HashMap<>();
        map.put("bucketExists", minIOUtil.bucketExists(bucketName));
        return ResultUtils.success(map);
    }

    @GetMapping("/makeBucket")
    @ApiOperation("创建存储桶")
    public BaseResponse makeBucket(@RequestParam("bucketName") String bucketName) {
        Map<String, Object> map = new HashMap<>();
        map.put("makeBucketSuccess", minIOUtil.makeBucket(bucketName));
        return ResultUtils.success( map);
    }

    @GetMapping("/removeBucket")
    @ApiOperation("删除存储桶")
    public BaseResponse removeBucket(@RequestParam("bucketName") String bucketName) {
        Map<String, Object> map = new HashMap<>();
        map.put("deleteBucketSuccess", minIOUtil.removeBucket(bucketName));
        return ResultUtils.success( map);
    }

    @GetMapping("/getAllBuckets")
    @ApiOperation("获取所有存储桶")
    public BaseResponse getAllBuckets() {
        Map<String, Object> map = new HashMap<>();
        map.put("buckets", minIOUtil.getAllBuckets());
        return ResultUtils.success( map);
    }

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public BaseResponse upload(@RequestParam("file") MultipartFile file) {
        String objectName = minIOUtil.upload(file);

        if (objectName != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("url", (minioConfig.getUrl() + "/" + minioConfig.getBucket() + "/" + objectName));
            return ResultUtils.success( map);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"上传失败");
    }

    @GetMapping("/preview")
    @ApiOperation("预览图片")
    public BaseResponse preview(@RequestParam("fileName") String fileName) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", minIOUtil.preview(fileName));
        return ResultUtils.success( map);
    }

    @GetMapping("/download")
    @ApiOperation("下载图片")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse resp) {
        minIOUtil.download(fileName, resp);
    }

    @PostMapping("/delete")
    @ApiOperation("删除图片")
    public BaseResponse remove(@RequestBody Map<String, String> params) {
        String url = params.get("url");
        String objName = url.substring(url.lastIndexOf(minioConfig.getBucket() + "/") + minioConfig.getBucket().length() + 1);
        log.info("删除对象: {}", objName);
        minIOUtil.remove(objName);
        return ResultUtils.success("删除成功");
    }
}

